package de.bomberman.world;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import de.bomberman.debug.Config;
import factory.MaterialFactory;
import java.util.logging.Logger;

/**
 *
 * @author Mats
 */
public class Field {

    private FIELD_TYPE type;
    private float size;
    private float height;
    private Geometry obstacleGeom;
    private RigidBodyControl physics;
    // Logger
    private static final Logger logger = Logger.getLogger(Field.class.getName());

    public Field(FIELD_TYPE type, float size, float height) {
        this.type = type;
        this.size = size;
        this.height = height;
    }

    public Geometry generateField(Vector3f location, AssetManager assetManager, FIELD_TYPE type) {
        Box obstacleBox = new Box(Vector3f.ZERO, size, height, size);
        obstacleGeom = new Geometry("obstacle", obstacleBox);

        Material obstacleMaterial = MaterialFactory.getLightMaterial(assetManager);

        // debug wireframe
        if (Config.DEBUG_WIREFRAME) {
            obstacleMaterial.getAdditionalRenderState().setWireframe(true);
        }
        obstacleGeom.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
        obstacleGeom.setMaterial(obstacleMaterial);

        Texture tex = null;
        if (type == FIELD_TYPE.BLOCK) {
            tex = assetManager.loadTexture("Textures/pflasterstein.jpg");
        } else if (type == FIELD_TYPE.WOOD) {
            tex = assetManager.loadTexture("Textures/wood.jpg");
        }

        tex.setWrap(Texture.WrapMode.Repeat);
        obstacleMaterial.setTexture("LightMap", tex);
        obstacleBox.scaleTextureCoordinates(new Vector2f(0.5f, 0.5f));

        // setup the physics
        physics = new RigidBodyControl(0f);
        obstacleGeom.addControl(physics);
        physics.setPhysicsLocation(location);

        return obstacleGeom;
    }

    public RigidBodyControl getPhysics() {
        return physics;
    }

    public Geometry getObstacle() {
        return obstacleGeom;
    }

    public FIELD_TYPE getType() {
        return type;
    }

    public void setType(FIELD_TYPE type) {
        this.type = type;
        if (type != FIELD_TYPE.EMPTY && type != FIELD_TYPE.BOMB) {
            obstacleGeom.removeControl(physics);
        }
    }

    public float getSize() {
        return size;
    }

    public void setSize(float size) {
        this.size = size;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }
}
